package cn.john.hrm.service.impl;

import cn.hutool.http.HttpUtil;
import cn.john.hrm.constant.AuthConstant;
import cn.john.hrm.domain.LoginUser;
import cn.john.hrm.domain.UserMeal;
import cn.john.hrm.dto.LoginUserDto;
import cn.john.hrm.dto.RefreshTokenDto;
import cn.john.hrm.mapper.LoginUserMapper;
import cn.john.hrm.mapper.UserMealMapper;
import cn.john.hrm.properties.Oauth2ClientDetails;
import cn.john.hrm.properties.Oauth2ClientDetailsProperties;
import cn.john.hrm.service.ILoginUserService;
import cn.john.hrm.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.text.RuleBasedCollator;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author john
 * @since 2021-06-17
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {
    @Autowired
    private UserMealMapper userMealMapper;

    @Override
    public boolean insert(LoginUser loginUser) {
        // 保存t_login_user
        super.insert(loginUser);
        // 保存t_user_meal mealId login_id
        UserMeal userMeal = new UserMeal();
        userMeal.setMealId(loginUser.getMealId());
        userMeal.setLoginId(loginUser.getId());
        userMealMapper.insert(userMeal);
        return true;
    }

    /*@Override
    public AjaxResult login(@Valid LoginUserDto dto) {
        String url = "http://localhost:3010/oauth/token?client_id=%s&client_secret=%s&grant_type=password&redirect_uri=http://www.baidu.com&username=%s&password=%s";
        // 根据用户类型获取相应的token
        if (dto.getType() == 0) {
            // 说明是后台管理员，发送请求获取对应的token
            url = String.format(url, "admin", "1", dto.getUsername(), dto.getPassword());
        } else {
            // 说明是前端用户
            url = String.format(url, "website", "1", dto.getUsername(), dto.getPassword());
        }
        String result = HttpUtil.get(url);
        Map map = JSON.parseObject(result, Map.class);
        return AjaxResult.me().setResultObj(map);
    }*/
    @Autowired
    private Oauth2ClientDetailsProperties properties;

    @Override
    public AjaxResult login(@Valid LoginUserDto dto) {
        // 根据类型 获取配置的对象
        Oauth2ClientDetails clientDetails = properties.getClientDetails(dto.getType());
        // 准备密码模式的url
        String url = String.format(
                AuthConstant.PWD_AUTH_URL,
                clientDetails.getClientid(),
                clientDetails.getSecret(),
                dto.getUsername(),
                dto.getPassword());
        String result = HttpUtil.get(url);
        Map map = JSON.parseObject(result, Map.class);
        return AjaxResult.me().setResultObj(map);
    }

    @Override
    public AjaxResult refresh(@Valid RefreshTokenDto dto) {
        // 根据类型 获取配置的对象
        Oauth2ClientDetails clientDetails = properties.getClientDetails(dto.getType());
        // 准备密码模式的url
        String url = String.format(
                AuthConstant.PWD_AUTH_REFRESH_URL,
                dto.getRefreshToken(),
                clientDetails.getClientid(),
                clientDetails.getSecret());
        String result = HttpUtil.get(url);
        Map map = JSON.parseObject(result, Map.class);
        return AjaxResult.me().setResultObj(map);
    }


}
